package com.itboye.jinpuyiyuan.activity.me;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.MeHosAdapter;
import com.itboye.jinpuyiyuan.adapter.MedicaRecordsAdapter;
import com.itboye.jinpuyiyuan.adapter.ReportAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.HospRecord;
import com.itboye.jinpuyiyuan.bean.OrderBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 就诊记录
 */
public class MedicaRecordsActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private SmartRefreshLayout refresh;
    private RecyclerView inHos;

    private List<HospRecord.Orders> list;
    private BranchAddressPresenter presenter;
    private MeHosAdapter meHosAdapter;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medica_records);

        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("就诊记录");
        list=new ArrayList<>();
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);


        meHosAdapter=new MeHosAdapter(this,list,1,"");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        inHos.setLayoutManager(linearLayoutManager);
        inHos.setAdapter(meHosAdapter);


        refresh.setEnableLoadmore(false);
//        WaveSwipeHeader waveSwipeHeader = new WaveSwipeHeader(this);
//        waveSwipeHeader.setColorSchemeColors(getResources().getColor(R.color.colorAccent));

        refresh.autoRefresh();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.getHospRecord(uid);
            }
        });

        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        ClassicsFooter classicsFooter = new ClassicsFooter(this);

        refresh.setRefreshHeader(classicsHeader);
        refresh.setRefreshFooter(classicsFooter);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case  R.id.rlLeft:
            case R.id.leftImg:
                finish();
                break;


        }
    }


    @Override
    public void update(Observable o, Object arg) {

        ResultEntity entity = handlerError(arg);

        if (entity!=null){
            refresh.finishRefresh();
            if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_visitingRecord_success){
                if (list.size()>0){
                    list.clear();
                }
                HospRecord bean= (HospRecord) entity.getData();
                list.addAll(bean.getOrder_list());
                meHosAdapter.addData(list,1,"");
            }else if (entity.getEventType()==BranchAddressPresenter.By_Hospitalization_visitingRecord_fail){
                MAlert.alert(entity.getMsg());
            }
        }
    }
}
